import 'package:flutter/material.dart';
import 'package:omni_calendar_view/omni_calendar_view.dart';
import 'package:unified_popups/unified_popups.dart';

import 'calendar_view.dart';

class CalendarPage extends StatefulWidget {
  const CalendarPage({super.key});

  @override
  State<CalendarPage> createState() => _CalendarPageState();
}

class _CalendarPageState extends State<CalendarPage> {

  // 1. 创建一个控制器
  late final OmniCalendarController controller;

  // 用于演示外部控制的状态变量
  final bool showLunar = false;
  final bool showSurroundingDays = true;
  late Locale locale = const Locale('en', 'EN');

  @override
  void initState() {
    super.initState();
    // 初始化控制器，可以传入初始日期
    controller = OmniCalendarController(
      initialDate: DateTime.now(),
    );
  }

  @override
  void dispose() {
    controller.dispose();
    super.dispose();
  }

  void _changeLocale(){
    if(locale == const Locale('en', 'EN')){
      setState(() {
        locale = const Locale('zh', 'CN');
      });
    }else{
      setState(() {
        locale = const Locale('en', 'EN');
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title:  const Text('Omni Calendar View Demo'),

      ),
      body: SingleChildScrollView(
        child: Padding(
          padding: const EdgeInsets.all(16.0),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              ElevatedButton(
                onPressed: (){
                  Navigator.of(context).pushNamed("/pop");
                },
                child: Text("弹框页面")
              ),
              ElevatedButton(
                onPressed: _changeLocale,
                child: Text("切换语言")
              ),
              CalendarView(
                controller: controller,
                showLunar: showLunar,
                locale: locale,
                showSurroundingDays: showSurroundingDays
              ),
              const SizedBox(height: 24),
              const Text("外部控制 & 状态查询", style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold)),
              const SizedBox(height: 12),
              Wrap(
                spacing: 8,
                runSpacing: 8,
                children: [
                  ElevatedButton(
                    onPressed: () {
                      // 3. 使用控制器跳转到指定日期
                      controller.jumpToDate(DateTime(2026, 1, 15));
                    },
                    child: const Text('跳转到 2026-01-15'),
                  ),
                  ElevatedButton(
                    onPressed: () {
                      // 4. 使用控制器查询当前状态
                      final singleDate = controller.selectedDate;
                      final range = controller.selectedDateRange;
                      String message;
                      if (range != null && controller.selectionType == SelectionType.range) {
                        message = '当前选择范围: ${range.start.toLocal().toString().split(' ')[0]} to ${range.end.toLocal().toString().split(' ')[0]}';
                      } else if (singleDate != null) {
                        message = '当前单选日期: ${singleDate.toLocal().toString().split(' ')[0]}';
                      } else {
                        message = '当前未选择任何日期';
                      }
                      showDialog(
                        context: context,
                        builder: (c) => AlertDialog(
                          title: const Text('当前日历状态'),
                          content: Text(message),
                          actions: [
                            TextButton(onPressed: () => Navigator.pop(c), child: const Text('好的'))
                          ],
                        ),
                      );
                    },
                    child: const Text('查询当前选择'),
                  ),
                  ElevatedButton(
                    onPressed:() {
                      PopupManager.show(
                        PopupConfig(
                          child: Container(
                            padding: EdgeInsets.symmetric(horizontal: 12),
                            margin: EdgeInsets.symmetric(horizontal: 12),
                            child: CalendarView(
                              controller: controller,
                              showLunar: showLunar,
                              locale: locale,
                              showSurroundingDays: showSurroundingDays
                            ),
                          )
                        )
                      );
                    },
                    child: const Text('弹框展示日历'),
                  ),
                ],
              ),
            ],
          ),
        ),
      ),
    );
  }
}
